'''
Author: kun 56216004@qq.com
Date: 2024-07-25 10:41:15
LastEditors: kun 56216004@qq.com
LastEditTime: 2024-07-25 11:52:54
FilePath: \langchain\12longTextSummary.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
# Azure OpenAI 入门教程 - LangChain 篇 ： 长文本内容总结 - 知乎
# https://zhuanlan.zhihu.com/p/646778848


from langchain.llms import OpenAI
import openai
openai.api_base = "https://api.chatanywhere.com.cn/v1"
from config import * 
# openai.api_key = api_key
import os
os.environ["OPENAI_API_KEY"] = api_key # 填入OpenAI Secret Key

llm = OpenAI(model_name="gpt-4o-mini")   #text-davinci-003


# from langchain.document_loaders import UnstructuredFileLoader
# loader = UnstructuredFileLoader("./data/产品经理.txt")
from langchain.document_loaders import TextLoader
loader = TextLoader('./data/产品经理.txt', encoding='utf8')
document=loader.load()
# Exception has occurred: BadZipFile
# File is not a zip file

from langchain.text_splitter import RecursiveCharacterTextSplitter
text_splitter =RecursiveCharacterTextSplitter(
    chunk_size =500,
    chunk_overlap=0
)


split_documents =text_splitter.split_documents(document)
print(f'documents:{len(split_documents)}')


from langchain.chains.summarize import load_summarize_chain
chain= load_summarize_chain(llm, chain_type="refine", verbose=True)
res=chain.run(split_documents[:5])


# C:\Users\kun-hp\.conda\envs\langchain01\Lib\site-packages\langchain\chains\summarize\refine_prompts.py
# Given the new context, refine the original summary
# If the context isn't useful, return the original summary.


# Your job is to produce a final summary
# We have provided an existing summary up to a certain point: The core responsibilities of a product manager are to ensure product success by addressing internal needs for optimal ROI and external demands for customer satisfaction and company profitability. Internally, they must conduct thorough research to understand the background, objectives, scope, estimated workload, and feasibility of demands, coordinating evaluations across departments to produce confirmed requirement documents that minimize discrepancies. This includes initiating meetings led by business units to secure project approval and establish overall and specific prioritizations.



# You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

# You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. 

# Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

# A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

print(res)
